package com.reader.cms.controller.sys;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.reader.cms.controller.BaseController;
import com.reader.cms.entity.sys.ReaderTags;
import com.reader.cms.mapper.sys.ReaderTagsMapper;
import com.reader.cms.service.sys.IReaderTagsService;
import com.reader.core.entity.ResultMessageInfo;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.reader.core.utils.ValidatorUtil;
import com.reader.core.validator.group.AddGroup;
import com.baomidou.mybatisplus.plugins.Page;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author gumenghao
 * @description 内容标签
 * @date 2018-10-25
 */
@RestController
@RequestMapping("/api/readertags")
public class ReaderTagsController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(ReaderTagsController.class);

    @Autowired
    public IReaderTagsService iReaderTagsService;


    @Autowired
    public ReaderTagsMapper readerTagsMapper;

    /**
     * @param currentPage 当前页
     * @param size        每页记录数
     * @param param       其它查询条件，根据情况动态添加
     * @return
     * @function 分页查询数据
     */
    @GetMapping("list")
    public ResultMessageInfo selectListPage(@RequestParam(value = "currentPage") int currentPage,
                                            @RequestParam(value = "size") int size,
                                            @RequestParam(value = "param", required = false) String param) {
        ResultMessageInfo resultMessage = null;
        Page<ReaderTags> list = iReaderTagsService.selectListPage(currentPage, size, "");
        return ResultMessageInfo.ofSuccess("查询成功！", list);
    }


    /**
     * 根据pid获取对应标签数据
     * @param pid 父级标签ID
     * @return　ResultMessageInfo
     * @function 查询对应等级的标签
     */
    @GetMapping("child")
    public ResultMessageInfo selectChild(@RequestParam(value = "pid", required = false) Integer pid) {
        ResultMessageInfo resultMessage = null;
        List<ReaderTags> list = iReaderTagsService.selectChild(pid);
        return ResultMessageInfo.ofSuccess("查询成功！", list);
    }

    /**
     * @param readerTags 实体对象
     * @return
     * @function 添加新标签
     */
    @PostMapping("insert")
    public ResultMessageInfo insert(@RequestBody ReaderTags readerTags) {
        ValidatorUtil.validateEntity(readerTags, AddGroup.class);//非空验证
        if (readerTags.getPid() == null) {
            readerTags.setPid(0);
        }
        EntityWrapper<ReaderTags> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag", 0).eq("name", readerTags.getName()).eq("pid", readerTags.getPid());
        Integer count = readerTagsMapper.selectCount(wrapper);
        if (count != 0) {
            if (readerTags.getPid() == 0) {
                return ResultMessageInfo.ofMessage("一级标签名称已存在！！！");
            } else {
                return ResultMessageInfo.ofMessage("二级标签名称已存在！！！");
            }
        }
        Integer userId = this.getUserId();
        readerTags.setInsertDt(new Date());
        readerTags.setEnable(1);
        readerTags.setDelFlag(0);
        readerTags.setInsertUserId(userId);
        readerTags.setUpdtUserId(userId);
        boolean resultValue = iReaderTagsService.insert(readerTags);
        if (!resultValue) {
            return ResultMessageInfo.ofMessage("添加失败！");
        }
        return ResultMessageInfo.ofSuccess("添加成功！");
    }

    /**
     * 更新标签数据
     * @param readerTags 实体对象
     * @return
     * @function 修改
     */
    @PostMapping("update")
    @Transactional
    public ResultMessageInfo update(@RequestBody ReaderTags readerTags) {
        ValidatorUtil.validateEntity(readerTags, AddGroup.class);//非空验证
        EntityWrapper<ReaderTags> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag", 0).eq("name",
                readerTags.getName()).eq("pid", readerTags.getPid()).ne("id", readerTags.getId());
        Integer count = readerTagsMapper.selectCount(wrapper);
        if (count != 0) {
            if (readerTags.getPid() == 0) {
                return ResultMessageInfo.ofMessage("一级标签名称已存在！！！");
            } else {
                return ResultMessageInfo.ofMessage("二级标签名称已存在！！！");
            }
        }
        Integer userId = this.getUserId();
        readerTags.setUpdtDt(new Date());
        readerTags.setUpdtUserId(userId);
        boolean resultValue = iReaderTagsService.updateById(readerTags);
        if (!resultValue) {
            return ResultMessageInfo.ofMessage("修改失败！");

        }
        return ResultMessageInfo.ofSuccess("修改成功！");
    }

    /**
     * @return
     * @function 删除单条标签
     */
    @PostMapping("delete")
    @Transactional
    public ResultMessageInfo deleteById(@RequestBody ReaderTags readerTags) {

        Integer userId = this.getUserId();
        readerTags.setDelFlag(1);
        readerTags.setUpdtDt(new Date());
        readerTags.setUpdtUserId(userId);
        boolean resultValue = iReaderTagsService.deleteById(readerTags.getId());
        if (!resultValue) {
            ResultMessageInfo.ofMessage("删除失败！");
        }
        return ResultMessageInfo.ofSuccess("删除成功！");

    }

    /**
     * @param data 键为ids值为整型数组的json格式
     * @return
     * @function 批量删除标签
     */
    @PostMapping("deleteIds")
    @Transactional
    public ResultMessageInfo deleteBatchIds(@RequestBody String data) {
        ResultMessageInfo resultMessage = null;
        List<Long> ids = JSON.parseArray(JSON.parseObject(data).getString("ids"), Long.class);
        boolean resultValue = iReaderTagsService.deleteBatchIds(ids);
        if (!resultValue) {
            ResultMessageInfo.ofMessage("删除失败！");
        }
        return ResultMessageInfo.ofSuccess("删除成功！");
    }
}